*******************************************
*** RUN JACKKNIFE REGS ON ALL HOSPITALS ***
*** Last edited: 4/19/2022         		***
*******************************************

log using "${CodePath}/log/regressions_all_$S_DATE.log", text replace
cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/hospcomp_all/"
cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/hospcomp_all/Event Studies/"
cap mkdir "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/hospcomp_all/Event Studies/CI95/"


*--------------------------------------------------
* Select and create dataset for regressions
*--------------------------------------------------

local hospvars    "log_n_claims" 

use "${DataPath}/derived/hospyear_0716_jk.dta", clear
keep pn fyear dist_to_border RACregion_audit* jk_RACregion_audit_in2011* jk_state_audit_in2011* state_audit_in2011* audit_in2011* jk_RACregion_audit_in2011* demandrate_in2011* jk_RACregion_demand_in2011*  jk_state_demand_in2011* `hospvars' nearseg100_state_2 nearseg100_state RACregion state demandafterauditrate_in2011
keep if fyear >= 2007 & fyear <= 2015

// restrict to strongly balanced panel
bys pn: gen max_n = _N
keep if max_n == 9
xtset pn fyear  

*--------------------------------------------------
* Event studies
*--------------------------------------------------

foreach hospvar of local hospvars{
	reghdfe `hospvar' jk_RACregion_audit_in2011_X2*, absorb(pn fyear) cluster(state)
	event_study95 jk_RACregion_audit_in2011

	graph export "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/hospcomp_all/Event Studies/CI95/`hospvar'.png", replace

	ivreghdfe `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
		 			= jk_RACregion_audit_in2011_X2007 jk_RACregion_audit_in2011_X2008 jk_RACregion_audit_in2011_X2009 jk_RACregion_audit_in2011_X2011 jk_RACregion_audit_in2011_X2012 jk_RACregion_audit_in2011_X2013 jk_RACregion_audit_in2011_X2014 jk_RACregion_audit_in2011_X2015), ///
		 			absorb(pn fyear) cluster(state)

	iv_event_study95 audit_in2011
	graph export "${OutputPath}/RACRegion/jackknife_RACregion_audit_in2011/hospcomp_all/Event Studies/CI95/`hospvar'_IV.png", replace

}


log close
